/*
*******************************************************************************
*                              (c) Copyright HighSun Techonology Company
*                                           All Rights Reserved
*
* File    : excitation_sample.c
* By      : HuangXi
* Version : V0.1
* Datetime: 2012-02-12 AM 09:50
* Description: Function define for excitation sample operation
*******************************************************************************
*/
#include <includes.h>
#include <math.h>

/*********************************** MACRO DEFINES ******************************************/
#define VOL_TRANSFORMER_AMP       (60)    /* Voltage amplifier of transformer */
#define CUR_TRANSFORMER_AMP       (1)    /* Current amplifier of transformer */

#define VOL_MUTUAL_INDUCTOR_AMP   (129.46)    /* Voltage amplifier of mudual inductor(include OPA) */
#define CUR_MUTUAL_INDUCTOR_AMP   (6.862*100.0)        /* Current amplifier of mudual inductor(include OPA) */
#define IL_RESISTOR               (56)

/************************************ Function Declares ********************************************/
static  inline  void cal_Uab(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para);
static  inline  void get_Ucb(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para);
static  inline  void get_Ia(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para);
static  inline  void get_Ic(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para);


typedef struct _excitation_sample_buff
{
    INT8U            RingBufRxCtr;              /* Number of completed sample(one include SAMPLE_POINT)in the RingBufRx */
    SAMPLE_BUFF     *RingBufRxInPtr;            /* Pointer to where next sample data will be inserted        */
    SAMPLE_BUFF     *RingBufRxOutPtr;           /* Pointer from where next sample data will be extracted     */
    SAMPLE_BUFF      RingBufRx[RING_BUFFER_SIZE];   /* Ring buffer character storage (Rx)                      */
}EXCITATION_SAMPLE_BUFF;




/* Define a instance of Excitation sample */
EXCITATION_SAMPLE_BUFF excitation_sample_buff;

/* ADC buffer init */
void init_adc_buff(void)
{
    excitation_sample_buff.RingBufRxCtr    = 0;
    excitation_sample_buff.RingBufRxInPtr  = &excitation_sample_buff.RingBufRx[0];
    excitation_sample_buff.RingBufRxOutPtr = &excitation_sample_buff.RingBufRx[0];
    mem_set(excitation_sample_buff.RingBufRx, 0, sizeof(excitation_sample_buff.RingBufRx));

}



/* Get the sample data  with 16 Channels */
void adc_get_data(SAMPLE_BUFF *buf_ptr, INT8U *err)
{
    INT8U        i       = 0;   
    INT8U        j       = 0;
    SAMPLE_BUFF  *out_ptr = (void *)0;
    
    OS_ENTER_CRITICAL();


    /* See if buff is empty */
    if (excitation_sample_buff.RingBufRxCtr > 0)
    {
        /* Is not empty,  Decrement the rx count */        
        excitation_sample_buff.RingBufRxCtr--;

        /* Get the data from buffer */
        out_ptr = excitation_sample_buff.RingBufRxOutPtr++;

        /* Sample points from 1 to 12 */
        for (i=0; i<SAMPLE_POINTS;i++)
        {
            /* Sample channel from 1 to 16 */
            for (j=0; j<SAMPLE_CHANNELS;j++)
            {
                buf_ptr->sample_buff[i].adc_result[j] = out_ptr->sample_buff[i].adc_result[j];
            }
        }


        /* Wrap out the pointer */
        if (excitation_sample_buff.RingBufRxOutPtr == (&excitation_sample_buff.RingBufRx[RING_BUFFER_SIZE]))
        {
            excitation_sample_buff.RingBufRxOutPtr = (&excitation_sample_buff.RingBufRx[0]);
        }

        *err = ADC_NO_ERR;

    }else{
        /* Buff is empty */
        *err = ADC_RX_EMPTY;
    }
    
    OS_EXIT_CRITICAL();
}

/* 
    See if have any completed sample data in ring buffer 
    empty -- TRUE   is    empty
             FALSE  have  data in the ring buffer 
*/
BOOLEAN adc_is_empty(void)
{
    BOOLEAN     empty  = FALSE;

    OS_ENTER_CRITICAL();
    if ((excitation_sample_buff.RingBufRxCtr) > 0)
    {
        empty = FALSE;
    }else{
        empty = TRUE;
    }

    OS_EXIT_CRITICAL();
    return (empty);
}


/*
    Insert sample data into ring buffer
*/
void adc_put_sample_data(const INT8U which_point, const INT8U channel, const Uint16 adc_result)
{
    SAMPLE_BUFF *in_ptr = (void *)0;
    
    /* See if the ring buffer is full */
    if (excitation_sample_buff.RingBufRxCtr < RING_BUFFER_SIZE)
    {
        /* No, put the adc result into ring buffer,increment the count */
        excitation_sample_buff.RingBufRxCtr++;

        /* Put the adc data into sample buffer */
        in_ptr = excitation_sample_buff.RingBufRxInPtr++;
        in_ptr->sample_buff[which_point].adc_result[channel] = adc_result;

        /* Wrap the pointer */
        if (excitation_sample_buff.RingBufRxInPtr == (&excitation_sample_buff.RingBufRx[RING_BUFFER_SIZE]))
        {
            excitation_sample_buff.RingBufRxInPtr = (&excitation_sample_buff.RingBufRx[0]);
        }
    }
}


/* Compute the parameter of Generator */
void compute_generator_parameter(GENERATOR_PARAMETER *generator_para)
{
    SAMPLE_BUFF sample_buffer;
    INT8U       err = 0;

    if (generator_para == ((void *)0))
    {
        return;
    }
    
    /* Get the ADC result for each Channel */
    //adc_get_data(&sample_buffer, &err);

    if (err != ADC_RX_EMPTY)
    {
        cal_Uab(&sample_buffer, generator_para);
      //  get_Ucb(&sample_buffer, generator_para);
      //  get_Ia(&sample_buffer, generator_para);
      //  get_Ic(&sample_buffer, generator_para);
    }
}


/* Calculate the Uab voltage */
static  inline  void cal_Uab(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para)
{
    Uint16 adc_result          = 0;
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.00};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    float64 Uab                = 0.00;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        adc_result = sample_buff_ptr->sample_buff[i].adc_result[UFAB];
        h[i]       = ((float64)(((float64)(adc_result)) / 4096) * 3.0);
    }

    ReU = ((float64)(1.0/6.0))*((h[0] - h[6])+(float64)(sqrt(3)/2.0)*(h[1]+h[11]-h[5]-h[7])+((float64)(1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = ((float64)(1.0/6.0))*((h[3] - h[9])+(float64)(sqrt(3)/2.0)*(h[2]+h[4]-h[8]-h[10])+((float64)(1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    Uab = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)(VOL_TRANSFORMER_AMP*VOL_MUTUAL_INDUCTOR_AMP));

    generator_para->Uab = Uab;

} 


/* Get the Ucb voltage */
static  inline  void get_Ucb(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para)
{
    Uint16 adc_result          = 0;
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.00};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    float64 Ucb                = 0.00;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        adc_result = sample_buff_ptr->sample_buff[i].adc_result[UFCB];
        h[i]       = ((float64)(((float64)(adc_result)) / 4096) * 3.0);
    }

    ReU = ((float64)(1.0/6.0))*((h[0] - h[6])+(float64)(sqrt(3)/2.0)*(h[1]+h[11]-h[5]-h[7])+((float64)(1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = ((float64)(1.0/6.0))*((h[3] - h[9])+(float64)(sqrt(3)/2.0)*(h[2]+h[4]-h[8]-h[10])+((float64)(1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    Ucb = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)(VOL_TRANSFORMER_AMP*VOL_MUTUAL_INDUCTOR_AMP));

    generator_para->Ucb = Ucb;
} 

/* Get the Ia current */
static  inline  void get_Ia(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para)
{
    Uint16 adc_result          = 0;
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.00};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    float64 Ia                 = 0.00;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        adc_result = sample_buff_ptr->sample_buff[i].adc_result[IFA];
        h[i]       = ((float64)(((float64)(adc_result)) / 4096) * 3.0);
    }

    ReU = ((float64)(1.0/6.0))*((h[0] - h[6])+(float64)(sqrt(3)/2.0)*(h[1]+h[11]-h[5]-h[7])+((float64)(1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = ((float64)(1.0/6.0))*((h[3] - h[9])+(float64)(sqrt(3)/2.0)*(h[2]+h[4]-h[8]-h[10])+((float64)(1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    Ia = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)(VOL_TRANSFORMER_AMP*VOL_MUTUAL_INDUCTOR_AMP));

    generator_para->Ia = Ia;
} 



/* Get the Ic current */
static  inline  void get_Ic(SAMPLE_BUFF *sample_buff_ptr, GENERATOR_PARAMETER *generator_para)
{
    Uint16 adc_result          = 0;
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.00};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    float64 Ic                 = 0.00;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        adc_result = sample_buff_ptr->sample_buff[i].adc_result[IFC];
        h[i]       = ((float64)(((float64)(adc_result)) / 4096) * 3.0);
    }

    ReU = ((float64)(1.0/6.0))*((h[0] - h[6])+(float64)(sqrt(3)/2.0)*(h[1]+h[11]-h[5]-h[7])+((float64)(1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = ((float64)(1.0/6.0))*((h[3] - h[9])+(float64)(sqrt(3)/2.0)*(h[2]+h[4]-h[8]-h[10])+((float64)(1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    Ic = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)(VOL_TRANSFORMER_AMP*VOL_MUTUAL_INDUCTOR_AMP));

    generator_para->Ic = Ic;
} 


/* For save the frequency value */
typedef struct _vol_Uab
{
    Uint16   vol_Uab;
    Uint16   vol_Ucb;
    Uint16   cur_Ia;
    Uint16   cur_Ic;
    float64  il;
    float64  ul;

    float64  ReUab;
    float64  ImUab;
    float64  ReUcb;
    float64  ImUcb;

    float64  ReIa;
    float64  ImIa;
    float64  ReIc;
    float64  ImIc;

    float64  g_p;
    float64  g_q;
    float64  g_s;
    float64  g_cos;
    
    float64 hi[SAMPLE_POINTS+1] ;
}VOL_Uab;

 static VOL_Uab vol_Uab;


/* Calculate the Uab voltage */
void debug_Uab(Uint16 *sample_buff_ptr)
{
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.0};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    Uint32 Uab                 = 0;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        h[i]       = (((float64)sample_buff_ptr[i] * 3.00) / 4096.00);

        #if (PRINT_POINTS == 1)
            vol_Uab.hi[i] = h[i];
        #endif
    }

   #if (SAMPLE_POINTS == TWELVE_POINTS)   
    ReU = (float64)(1.414/12.0)*((h[0] - h[6])+(0.866)*(h[1]+h[11]-h[5]-h[7])+((1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = (float64)(1.414/12.0)*((h[3] - h[9])+(0.866)*(h[2]+h[4]-h[8]-h[10])+((1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    //  ReU = (float64)(1.0/6.0)*((h[11] - h[5])+(0.866)*(h[0]+h[10]-h[4]-h[6])+((1.0/2.0))*(h[1]+h[9]-h[3]-h[7]));
   // ImU = (float64)(1.0/6.0)*((h[2] - h[8])+(0.866)*(h[3]+h[1]-h[7]-h[9])+((1.0/2.0))*(h[4]+h[0]-h[6]-h[10]));
   #else
 
    ReU = (float64)(1.414/32.0)*((h[0]-h[16])+0.980785*(h[1]-h[15]-h[17]+h[31])+0.923880*(h[2]-h[14]-h[18]+h[30])
         +0.831470*(h[3]-h[13]-h[19]+h[29])+ 0.707107*(h[4]-h[12]-h[20]+h[28])
         + 0.555570*(h[5]-h[11]-h[21]+h[27])+ 0.382683*(h[6]-h[10]-h[22]+h[26])
         + 0.195090*(h[7]-h[9]-h[23]+h[25]));


    ImU = (float64)(1.414/32.0)*(0.195090*(h[1]+h[15]-h[17]-h[31])+0.382683*(h[2]+h[14]-h[18]-h[30])+ 0.555570*(h[3]+h[13]-h[19]-h[29])
        + 0.707107*(h[4]+h[12]-h[20]-h[28])+0.831470*(h[5]+h[11]-h[21]-h[27])+0.923880*(h[6]+h[10]-h[22]-h[26])
        +0.980785*(h[7]+h[9]-h[23]-h[25])+h[8]-h[24]);
    #endif



    //(float64)(1.0/6.0)*
    Uab = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)((float64)VOL_TRANSFORMER_AMP*(float64)VOL_MUTUAL_INDUCTOR_AMP));


    vol_Uab.ReUab    = (float64)ReU;
    vol_Uab.ImUab    = (float64)ImU;
    vol_Uab.vol_Uab  = Uab;
} 




/* Calculate the Uab voltage */
void debug_Ucb(Uint16 *sample_buff_ptr)
{
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.0};
    float64 ReU                = 0.00;
    float64 ImU                = 0.00;
    Uint32 Ucb                 = 0;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        h[i]       = (((float64)sample_buff_ptr[i] * 3.00) / 4096.00);

        #if (PRINT_POINTS == 1)
            //vol_Uab.hi[i] = h[i];
        #endif
    }

   #if (SAMPLE_POINTS == TWELVE_POINTS)   
    ReU = (float64)(1.414/12.0)*((h[0] - h[6])+(0.866)*(h[1]+h[11]-h[5]-h[7])+((1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImU = (float64)(1.414/12.0)*((h[3] - h[9])+(0.866)*(h[2]+h[4]-h[8]-h[10])+((1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    //  ReU = (float64)(1.0/6.0)*((h[11] - h[5])+(0.866)*(h[0]+h[10]-h[4]-h[6])+((1.0/2.0))*(h[1]+h[9]-h[3]-h[7]));
   // ImU = (float64)(1.0/6.0)*((h[2] - h[8])+(0.866)*(h[3]+h[1]-h[7]-h[9])+((1.0/2.0))*(h[4]+h[0]-h[6]-h[10]));
   #else
 
    ReU = (float64)(1.414/32.0)*((h[0]-h[16])+0.980785*(h[1]-h[15]-h[17]+h[31])+0.923880*(h[2]-h[14]-h[18]+h[30])
         +0.831470*(h[3]-h[13]-h[19]+h[29])+ 0.707107*(h[4]-h[12]-h[20]+h[28])
         + 0.555570*(h[5]-h[11]-h[21]+h[27])+ 0.382683*(h[6]-h[10]-h[22]+h[26])
         + 0.195090*(h[7]-h[9]-h[23]+h[25]));


    ImU = (float64)(1.414/32.0)*(0.195090*(h[1]+h[15]-h[17]-h[31])+0.382683*(h[2]+h[14]-h[18]-h[30])+ 0.555570*(h[3]+h[13]-h[19]-h[29])
        + 0.707107*(h[4]+h[12]-h[20]-h[28])+0.831470*(h[5]+h[11]-h[21]-h[27])+0.923880*(h[6]+h[10]-h[22]-h[26])
        +0.980785*(h[7]+h[9]-h[23]-h[25])+h[8]-h[24]);
    #endif



    //(float64)(1.0/6.0)*
    Ucb = sqrt((ReU*ReU) + (ImU*ImU)) * ((float64)((float64)VOL_TRANSFORMER_AMP*(float64)VOL_MUTUAL_INDUCTOR_AMP));

    vol_Uab.ReUcb    = (float64)ReU;
    vol_Uab.ImUcb    = (float64)ImU;
    vol_Uab.vol_Ucb  = Ucb;
} 

/* Calculate the Ia current */
void debug_Ia(Uint16 *sample_buff_ptr)
{
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.0};
    float64 ReI                = 0.00;
    float64 ImI                = 0.00;
    Uint32  Ia                 = 0;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        h[i]       = (((float64)sample_buff_ptr[i] * 3.00) / 4096.00);

        #if (PRINT_POINTS == 1)
            //vol_Uab.hi[i] = h[i];
        #endif
    }

   #if (SAMPLE_POINTS == TWELVE_POINTS)   
    ReI = (float64)(1.414/12.0)*((h[0] - h[6])+(0.866)*(h[1]+h[11]-h[5]-h[7])+((1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImI = (float64)(1.414/12.0)*((h[3] - h[9])+(0.866)*(h[2]+h[4]-h[8]-h[10])+((1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    //  ReU = (float64)(1.0/6.0)*((h[11] - h[5])+(0.866)*(h[0]+h[10]-h[4]-h[6])+((1.0/2.0))*(h[1]+h[9]-h[3]-h[7]));
   // ImU = (float64)(1.0/6.0)*((h[2] - h[8])+(0.866)*(h[3]+h[1]-h[7]-h[9])+((1.0/2.0))*(h[4]+h[0]-h[6]-h[10]));
   #else
 
    ReI = (float64)(1.414/32.0)*((h[0]-h[16])+0.980785*(h[1]-h[15]-h[17]+h[31])+0.923880*(h[2]-h[14]-h[18]+h[30])
         +0.831470*(h[3]-h[13]-h[19]+h[29])+ 0.707107*(h[4]-h[12]-h[20]+h[28])
         + 0.555570*(h[5]-h[11]-h[21]+h[27])+ 0.382683*(h[6]-h[10]-h[22]+h[26])
         + 0.195090*(h[7]-h[9]-h[23]+h[25]));


    ImI = (float64)(1.414/32.0)*(0.195090*(h[1]+h[15]-h[17]-h[31])+0.382683*(h[2]+h[14]-h[18]-h[30])+ 0.555570*(h[3]+h[13]-h[19]-h[29])
        + 0.707107*(h[4]+h[12]-h[20]-h[28])+0.831470*(h[5]+h[11]-h[21]-h[27])+0.923880*(h[6]+h[10]-h[22]-h[26])
        +0.980785*(h[7]+h[9]-h[23]-h[25])+h[8]-h[24]);
    #endif



    //(float64)(1.0/6.0)*
    Ia = sqrt((ReI*ReI) + (ImI*ImI)) * ((float64)((float64)CUR_TRANSFORMER_AMP*(float64)CUR_MUTUAL_INDUCTOR_AMP));


    vol_Uab.ReIa    = (float64)ReI;
    vol_Uab.ImIa    = (float64)ImI;
    vol_Uab.cur_Ia  = Ia;
} 


/* Calculate the Ic current */
void debug_Ic(Uint16 *sample_buff_ptr)
{
    Uint16 i                   = 0;
    float64 h[SAMPLE_POINTS+1] = {0.0};
    float64 ReI                = 0.00;
    float64 ImI                = 0.00;
    Uint32  Ic                 = 0;

    for (i = 0; i < SAMPLE_POINTS; i++)
    {
        h[i]       = (((float64)sample_buff_ptr[i] * 3.00) / 4096.00);

        #if (PRINT_POINTS == 1)
            //vol_Uab.hi[i] = h[i];
        #endif
    }

   #if (SAMPLE_POINTS == TWELVE_POINTS)   
    ReI = (float64)(1.414/12.0)*((h[0] - h[6])+(0.866)*(h[1]+h[11]-h[5]-h[7])+((1.0/2.0))*(h[2]+h[10]-h[4]-h[8]));
    ImI = (float64)(1.414/12.0)*((h[3] - h[9])+(0.866)*(h[2]+h[4]-h[8]-h[10])+((1.0/2.0))*(h[1]+h[5]-h[7]-h[11]));

    //  ReU = (float64)(1.0/6.0)*((h[11] - h[5])+(0.866)*(h[0]+h[10]-h[4]-h[6])+((1.0/2.0))*(h[1]+h[9]-h[3]-h[7]));
   // ImU = (float64)(1.0/6.0)*((h[2] - h[8])+(0.866)*(h[3]+h[1]-h[7]-h[9])+((1.0/2.0))*(h[4]+h[0]-h[6]-h[10]));
   #else
 
    ReI = (float64)(1.414/32.0)*((h[0]-h[16])+0.980785*(h[1]-h[15]-h[17]+h[31])+0.923880*(h[2]-h[14]-h[18]+h[30])
         +0.831470*(h[3]-h[13]-h[19]+h[29])+ 0.707107*(h[4]-h[12]-h[20]+h[28])
         + 0.555570*(h[5]-h[11]-h[21]+h[27])+ 0.382683*(h[6]-h[10]-h[22]+h[26])
         + 0.195090*(h[7]-h[9]-h[23]+h[25]));


    ImI = (float64)(1.414/32.0)*(0.195090*(h[1]+h[15]-h[17]-h[31])+0.382683*(h[2]+h[14]-h[18]-h[30])+ 0.555570*(h[3]+h[13]-h[19]-h[29])
        + 0.707107*(h[4]+h[12]-h[20]-h[28])+0.831470*(h[5]+h[11]-h[21]-h[27])+0.923880*(h[6]+h[10]-h[22]-h[26])
        +0.980785*(h[7]+h[9]-h[23]-h[25])+h[8]-h[24]);
    #endif



    //(float64)(1.0/6.0)*
    Ic = sqrt((ReI*ReI) + (ImI*ImI)) * ((float64)((float64)CUR_TRANSFORMER_AMP*(float64)CUR_MUTUAL_INDUCTOR_AMP));


    vol_Uab.ReIc    = (float64)ReI;
    vol_Uab.ImIc    = (float64)ImI;
    vol_Uab.cur_Ic  = Ic;
} 


void debug_IL(Uint16 il_ad)
{
    vol_Uab.il = (((float64)il_ad * 3.00) / 4096.00) / ((float64)IL_RESISTOR);
}




void debug_UL(Uint16 ul_ad)
{
    vol_Uab.ul = (((float64)ul_ad * 3.00) / 4096.00);
}






/* Calculate the P,Q,S, cosa */
void cal_P_Q_S(void)
{
#if 0
    vol_Uab.g_p = (float64)((vol_Uab.ReUab*vol_Uab.ReIa)+(vol_Uab.ImUab*vol_Uab.ImIa)+(vol_Uab.ReUcb*vol_Uab.ReIc)+(vol_Uab.ImUcb*vol_Uab.ImIc));
    vol_Uab.g_q = (float64)((vol_Uab.ImUab*vol_Uab.ReIa)-(vol_Uab.ReUab*vol_Uab.ImIa)+(vol_Uab.ImUcb*vol_Uab.ReIc)-(vol_Uab.ReUcb*vol_Uab.ImIc));

    vol_Uab.g_s = (float64)sqrt((vol_Uab.g_p*vol_Uab.g_p)+(vol_Uab.g_q*vol_Uab.g_q));
    vol_Uab.g_cos = (float64)(vol_Uab.g_p/vol_Uab.g_s);
#endif

    vol_Uab.g_p = (float64)((vol_Uab.ReUab*vol_Uab.ReIc)+(vol_Uab.ImUab*vol_Uab.ImIc));
    vol_Uab.g_q = (float64)((vol_Uab.ImUab*vol_Uab.ReIc)-(vol_Uab.ReUab*vol_Uab.ImIc));

    vol_Uab.g_s = (float64)sqrt((vol_Uab.g_p*vol_Uab.g_p)+(vol_Uab.g_q*vol_Uab.g_q));
    vol_Uab.g_cos = (float64)(vol_Uab.g_p/vol_Uab.g_s);
}

Uint32 get_P(void)
{
    return (vol_Uab.g_p * 1000);
}


Uint32 get_Q(void)
{
    return (vol_Uab.g_q * 1000);
}

Uint32 get_S(void)
{
    return (vol_Uab.g_s * 1000);
}


Uint32 get_cos(void)
{
    return (vol_Uab.g_cos * 100);
}

Uint32 get_vol_Uab(void)
{
    return vol_Uab.vol_Uab;
}

Uint32 get_vol_Ucb(void)
{
    return vol_Uab.vol_Ucb;
}

Uint32 get_cur_Ia(void)
{
    return vol_Uab.cur_Ia;
}

Uint32 get_cur_Ic(void)
{
    return vol_Uab.cur_Ic;
}


Uint32 get_il(void)
{
    return (Uint32)(vol_Uab.il*1000);
}



Uint32 get_ul(void)
{
    return (Uint32)(vol_Uab.ul*100);
}

void print_points(void)
{
   #if (SAMPLE_POINTS == TWELVE_POINTS)
    debug_print("%s%ld%s","0:",(Uint32)(vol_Uab.hi[0]*1000),"\n\r");
    debug_print("%s%ld%s","1:",(Uint32)(vol_Uab.hi[1]*1000),"\n\r");
    debug_print("%s%ld%s","2:",(Uint32)(vol_Uab.hi[2]*1000),"\n\r");
    debug_print("%s%ld%s","3:",(Uint32)(vol_Uab.hi[3]*1000),"\n\r");
    debug_print("%s%ld%s","4:",(Uint32)(vol_Uab.hi[4]*1000),"\n\r");
    debug_print("%s%ld%s","5:",(Uint32)(vol_Uab.hi[5]*1000),"\n\r");
    debug_print("%s%ld%s","6:",(Uint32)(vol_Uab.hi[6]*1000),"\n\r");
    debug_print("%s%ld%s","7:",(Uint32)(vol_Uab.hi[7]*1000),"\n\r");
    debug_print("%s%ld%s","8:",(Uint32)(vol_Uab.hi[8]*1000),"\n\r");
    debug_print("%s%ld%s","9:",(Uint32)(vol_Uab.hi[9]*1000),"\n\r");
    debug_print("%s%ld%s","10:",(Uint32)(vol_Uab.hi[10]*1000),"\n\r");
    debug_print("%s%ld%s","11:",(Uint32)(vol_Uab.hi[11]*1000),"\n\r");
  #else
    debug_print("%s%ld%s","0:",(Uint32)(vol_Uab.hi[0]*1000),"\n\r");
    debug_print("%s%ld%s","1:",(Uint32)(vol_Uab.hi[1]*1000),"\n\r");
    debug_print("%s%ld%s","2:",(Uint32)(vol_Uab.hi[2]*1000),"\n\r");
    debug_print("%s%ld%s","3:",(Uint32)(vol_Uab.hi[3]*1000),"\n\r");
    debug_print("%s%ld%s","4:",(Uint32)(vol_Uab.hi[4]*1000),"\n\r");
    debug_print("%s%ld%s","5:",(Uint32)(vol_Uab.hi[5]*1000),"\n\r");
    debug_print("%s%ld%s","6:",(Uint32)(vol_Uab.hi[6]*1000),"\n\r");
    debug_print("%s%ld%s","7:",(Uint32)(vol_Uab.hi[7]*1000),"\n\r");
    debug_print("%s%ld%s","8:",(Uint32)(vol_Uab.hi[8]*1000),"\n\r");
    debug_print("%s%ld%s","9:",(Uint32)(vol_Uab.hi[9]*1000),"\n\r");
    debug_print("%s%ld%s","10:",(Uint32)(vol_Uab.hi[10]*1000),"\n\r");
    debug_print("%s%ld%s","11:",(Uint32)(vol_Uab.hi[11]*1000),"\n\r");

    debug_print("%s%ld%s","12:",(Uint32)(vol_Uab.hi[12]*1000),"\n\r");
    debug_print("%s%ld%s","13:",(Uint32)(vol_Uab.hi[13]*1000),"\n\r");
    debug_print("%s%ld%s","14:",(Uint32)(vol_Uab.hi[14]*1000),"\n\r");
    debug_print("%s%ld%s","15:",(Uint32)(vol_Uab.hi[15]*1000),"\n\r");
    debug_print("%s%ld%s","16:",(Uint32)(vol_Uab.hi[16]*1000),"\n\r");
    debug_print("%s%ld%s","17:",(Uint32)(vol_Uab.hi[17]*1000),"\n\r");
    debug_print("%s%ld%s","18:",(Uint32)(vol_Uab.hi[18]*1000),"\n\r");
    debug_print("%s%ld%s","19:",(Uint32)(vol_Uab.hi[19]*1000),"\n\r");
    debug_print("%s%ld%s","20:",(Uint32)(vol_Uab.hi[20]*1000),"\n\r");
    debug_print("%s%ld%s","21:",(Uint32)(vol_Uab.hi[21]*1000),"\n\r");
    debug_print("%s%ld%s","22:",(Uint32)(vol_Uab.hi[22]*1000),"\n\r");
    debug_print("%s%ld%s","23:",(Uint32)(vol_Uab.hi[23]*1000),"\n\r");

    debug_print("%s%ld%s","24:",(Uint32)(vol_Uab.hi[24]*1000),"\n\r");
    debug_print("%s%ld%s","25:",(Uint32)(vol_Uab.hi[25]*1000),"\n\r");
    debug_print("%s%ld%s","26:",(Uint32)(vol_Uab.hi[26]*1000),"\n\r");
    debug_print("%s%ld%s","27:",(Uint32)(vol_Uab.hi[27]*1000),"\n\r");
    debug_print("%s%ld%s","28:",(Uint32)(vol_Uab.hi[28]*1000),"\n\r");
    debug_print("%s%ld%s","29:",(Uint32)(vol_Uab.hi[29]*1000),"\n\r");
    debug_print("%s%ld%s","30:",(Uint32)(vol_Uab.hi[30]*1000),"\n\r");
    debug_print("%s%ld%s","31:",(Uint32)(vol_Uab.hi[31]*1000),"\n\r");


  #endif


    
}
